package app.netant.book.wanshu;

import org.jsoup.select.Elements;

import app.netant.NetAntBase;
import app.portal.system.sysmsg.sysMessage;

public class WanshuNetDetail extends NetAntBase {

	private WanshuEntity entity;
	
	public WanshuNetDetail(String netSite) {
		super(netSite);
	}
	
	public void setEntity(WanshuEntity entity){
		this.entity = entity;
	}
	
	public void analyseTitle(){
		
		Elements activePart = document.select("#maininfo #bookinfo .bookright .d_title");
		if(activePart.size()>0){
			
			String title = activePart.get(0).select("h1").text().trim();
			String writer = activePart.get(0).select(".p_author").text().trim();
			
			int index = writer.indexOf("：");
			if(index > 0){
				writer = writer.substring(index+1).trim();
			}
			
			System.out.println("【Title】"+title);
			System.out.println("【Writer】"+writer);
			
			this.entity.setNovelName(title);
			this.entity.setNovelFullName(title+".txt");
			this.entity.setNovelWriter(writer);
			
		}
		
	}
	
	public void analyseMemo(){
		
		Elements activePart = document.select("#maininfo #bookinfo .bookright #bookintro");
		
		if(activePart.size()>0){			
			String memo = activePart.get(0).text();
			System.out.println("【Memo】"+memo);	
			this.entity.setNovelMemo(memo);
		}
		
	}
	
	public void analyseDate(){
		
		Elements activePart = document.select("#maininfo #bookinfo .bookright #keywords");
		if(activePart.size()>0){	
			
			String date = activePart.get(0).text();
			int index = date.indexOf("：");
			if(index > 0){
				date = date.substring(index+1).trim();
			}
			
			System.out.println("【Date】"+date);	
			
			this.entity.setNovelDate(date);
			
		}
		
	}
	
	public void analyseTag(){
		
		Elements activePart = document.select("#maininfo #bookinfo .bookright #count");
		if(activePart.size()>0){
			
			String tags = activePart.get(0).text();
			
			String[] tagm = tags.split("：");
			String keys = "";
			String vals = "";
			
			for(int i=0;i<tagm.length;i++){
				
				String tmp = tagm[i].trim();
				
				if(i==0){
					keys += "," + tmp.replaceAll(" ", "");
				}
				else if(i==tagm.length-1){
					vals += "," + tmp.replaceAll(" ", "");
				}
				else{
					
					int first = tmp.indexOf(" ");
					if(first>0){
						keys += "," + tmp.substring(first).replaceAll(" ", "");
						vals += "," + tmp.substring(0,first).replaceAll(" ", "");
					}
					
				}
				
			}
			
			if(!"".equals(keys)){
				keys = keys.substring(1);
				vals = vals.substring(1);
			}
			
			String[] keyS = keys.split(",");
			String[] valS = vals.split(",");
			
			for(int j=0;j<keyS.length;j++){
				
				System.out.println("【Tag】"+keyS[j]+"="+valS[j]);
				
				this.entity.setTag(keyS[j], valS[j]);
				
			}
			
		}
		
		
	}
	
	
	public void analyseHref(){
		
		Elements activePart = document.select("#maininfo #bookinfo #button_all .li3 a");
		if(activePart.size()>0){
			String href = activePart.get(0).attr("href");
			
			if(href.startsWith("/modules")){
				href = "https://www.wanshu5.net" + href;
			}
			
			System.out.println("【Href】"+href);
			
			this.entity.setDownPath(href);
			
		}
		
	}
	
	public void analyse(){
		
		this.analyseTitle();
		this.analyseTag();
		this.analyseMemo();
		this.analyseHref();
		this.analyseDate();
		
		this.entity.download();
		this.entity.insert();
		
	}
	
	public static void main(String[] args) {
		WanshuNetDetail detail = new WanshuNetDetail("http://www.wanshu5.net/book/173178.html");
		detail.analyseTitle();
		detail.analyseMemo();
		detail.analyseDate();
		detail.analyseTag();
		detail.analyseHref();
	}

}
